package com.ymatou.iapi.trading.userquery.service;

import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ymatou.query.seller.trading.api.parameter.GetSingleOrderInfoResponse;
import com.ymt.base.YmatouBaseCall;
import com.ymt.core.tool.FormatUtil;
import com.ymttest.common.order.userquery.model.vo.SingleOrderInfo;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.DateDeserializer;
import com.ymttest.utils.EnvSetup;

public class GetHunterSingleOrderInfoCall extends YmatouBaseCall {
	
	private static String opurl = "api/hunter/getHunterSingleOrderInfo";

	public GetHunterSingleOrderInfoCall() {
		super(EnvSetup.getSystemUrl("userquery_iapi"), opurl, "GET", "PARAM");
	}

	@Override
	public void addDefHeader() {
		client.addHeader("Accept",
				"text/xml,text/javascript,text/html,application/json");
		client.addHeader("Content-Type", "application/json; charset=UTF-8");
	}

	/**
	 * 获取Code
	 * 
	 * @return
	 */
	public String getCode() {
		String Code = null;
		try {
			Code = new JSONObject(getReturnData()).optString("code");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Code;
	}

	/**
	 * 获取BCode
	 * 
	 * @return
	 */
	public String getBCode() {
		String BCode = null;
		try {
			BCode = new JSONObject(getReturnData()).optString("bcode");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return BCode;
	}

	/**
	 * 获取Msg
	 * 
	 * @return
	 */
	public String getMsg() {
		String BCode = null;
		try {
			BCode = new JSONObject(getReturnData()).optString("msg");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return BCode;
	}

	/**
	 * 获取Data
	 * 
	 * @return
	 */
	public JSONObject getOrderInfo() {
		JSONObject Data = null;
		try {
			Data = new JSONObject(getReturnData()).getJSONObject("data");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Data;
	}

	public GetSingleOrderInfoResponse getSingleOrderInfoResponse() {

		return (GetSingleOrderInfoResponse) FormatUtil.GSONToObject(
				DataUtil.Json.parseJsonObj(getOrderInfo().toString())
						.toString(), GetSingleOrderInfoResponse.class);
	}

	public SingleOrderInfo getSingleOrderInfo() throws JSONException {

		GsonBuilder gsonBuilder = new GsonBuilder();
		gsonBuilder.registerTypeAdapter(Date.class, new DateDeserializer());
		Gson gson = gsonBuilder.create();

		return (SingleOrderInfo) gson.fromJson(getOrderInfo().optString("orderInfo"),
				SingleOrderInfo.class);
	}
}
